/*
 * This file is part of "Deathwatch Ammo Counter for Android".
 *
 * "Deathwatch Ammo Counter for Android" is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * "Deathwatch Ammo Counter for Android" is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with "Deathwatch Ammo Counter for Android".  
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.TikiiTorch.Deathwatch.AmmoCounter.file;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.TikiiTorch.Deathwatch.AmmoCounter.Ammunition;
import com.TikiiTorch.Deathwatch.AmmoCounter.C;

public class AmmoHandler extends DefaultHandler {

	ArrayList<Ammunition> xmlMatches = null;

	String gunType;

	String name;
	String effect;
	String type;

	boolean bEffect = false;
	boolean bType = false;

	public AmmoHandler(String gunType) {
		this.gunType = gunType;
		xmlMatches = new ArrayList<Ammunition>();
		name = "";
	}

	public Ammunition[] getMatches() {
		Ammunition res[] = new Ammunition[xmlMatches.size()];

		for (int i = 0; i < xmlMatches.size(); i++) {
			res[i] = xmlMatches.get(i);
		}

		return res;

	}

	@Override
	public void startDocument() throws SAXException {
		// some sort of setting up work
	}

	@Override
	public void endDocument() throws SAXException {
		// some sort of finishing up work
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {

		if (qName.equalsIgnoreCase("ammo")) {
			// in an "ammo" node
			name = atts.getValue("name");
			// Log.d(C.TAG, name);
		}
		if (qName.equalsIgnoreCase("effect")) {
			bEffect = true;
		}
		if (qName.equalsIgnoreCase("type")) {
			bType = true;
		}
	}

	@Override
	public void characters(char ch[], int start, int length) {
		if (bEffect) {
			effect = new String(ch, start, length);
			bEffect = false;
		}
		if (bType) {
			type = new String(ch, start, length);
			bType = false;
		}
	}

	/**
	 * Occurs when the parser reaches the end of an element
	 */
	@Override
	public void endElement(String namesaceURI, String localName, String qName)
			throws SAXException {

		if (qName.equalsIgnoreCase("ammo")) {
			// Log.d(C.TAG, name + " - " + type);
			// end of an ammo element
			if (type.equalsIgnoreCase(gunType)) {
				Ammunition ammo = new Ammunition(name, effect);
				Log.d(C.TAG, "Adding: " + ammo.getName());
				xmlMatches.add(ammo);
			}
			name = "";
			effect = "";
			type = "";
		}

	}

}
